<template>
  <div class="main">
    <div class="box">
      <div class="top">
        <h2>文言一心</h2>
        <el-button type="error" @click="clear">清空聊天</el-button>
        <el-scrollbar class="scrollbar">
          <p
            v-for="item in chatList.messages"
            :key="item"
            class="scrollbar-demo-item"
          >
            <div class="left"  v-show="item.role == 'assistant'" v-html="'人工智能：'+item.content "></div>
            <div class="right"  v-show="item.role == 'user'" v-html="'用户：'+item.content"></div>
          </p>
        </el-scrollbar>
      </div>
      <div class="bottom">
        <el-input v-model="chat" style="width: 60%" placeholder="请输入会话内容" />
        <el-button type="success" @click="Chat" :disabled="load">聊天</el-button>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import axios from "axios";
import { ElMessage } from "element-plus";
import { ref } from "vue";

const load =ref(false)
const chat = ref("");
const chatList = ref({
  messages: [],
});
const clear = () => {
  chatList.value.messages = []
  ElMessage({
    type: 'success',
    message:"清除聊天成功"
  })
  load.value=false
}

const Chat = () => {
    load.value=true
  chatList.value.messages.push({
    role: "user",
    content: chat.value,
  });
  const req = {
    str: JSON.stringify(chatList.value),
    id: 233,
  };
  axios
    .post("/api/ai/chat", req)
    .then((res) => {
      console.log("result", res);
      const msg = res.data;
      chatList.value.messages.push({
        role: "assistant",
        content: msg.result,
      });
        chat.value = ""
      
      load.value = false
        
    })
    .catch((err) => {
      console.log("err", err);
    });
};
</script>

<style lang="scss" scoped>
.main {
  height: 100%;
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  .box {
    width: 80%;
    .top {
      height: 80vh;
      width: 100%;
      display: flex;
      flex-direction: column;
      align-items: center;
      .scrollbar {
        height: 90%;
      }
      .scrollbar-demo-item {
        display: flex;
        align-items: center;
        justify-content: center;
        height: 50px;
        margin: 10px;
        text-align: center;
        border-radius: 4px;

        color: var(--el-color-primary);
        
      }
      .left {
          text-align: left;
          display: flex;
          align-items: center;
          
        }
        .rigth {
          text-align: rigth;
          display: flex;
          align-content: center;
        }
    }
    .bottom {
      height: 10vh;
      display: flex;
      align-items: center;
      justify-content: space-around;
    }
  }
}
</style>
